package com.kunbo.cn.service.impl.core.purchase;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kunbo.cn.dao.core.purchase.AmountOfModulationHistoryDao;
import com.kunbo.cn.dao.core.purchase.ProcurementAdjustmentDao;
import com.kunbo.cn.entity.core.purchase.ProcurementAdjustment;
import com.kunbo.cn.service.core.purchase.ProcurementAdjustmentService;

@Service
public class ProcurementAdjustmentImpl implements ProcurementAdjustmentService {

	@Autowired
	private ProcurementAdjustmentDao dao;
	
	@Autowired
	private AmountOfModulationHistoryDao  historyDao;
	
	@Autowired
	private JdbcTemplate jdbc;


	@Override
	public Boolean update(Integer id, String order_code, String receiving_unit, String supplier,
			String material_classification, String material, String settlement_method, Float adjustment_amount,
			Float number_of_contracts, String remarks) {
		// TODO Auto-generated method stub
		ProcurementAdjustment adjustment = dao.findOne(id);
		adjustment.setAdjustment_amount(adjustment_amount);
		adjustment.setMaterial(material);
		adjustment.setMaterial_classification(material_classification);
		adjustment.setNumber_of_contracts(number_of_contracts);
		adjustment.setOrder_code(order_code);
		adjustment.setReceiving_unit(receiving_unit);
		adjustment.setRemarks(remarks);
		adjustment.setSettlement_method(settlement_method);
		adjustment.setSupplier(supplier);
		adjustment = dao.save(adjustment);
		if (adjustment != null)
			return true;
		else
			return false;
	}
	@Transactional
	@Override
	public ProcurementAdjustment sel(Integer id) {
		// TODO Auto-generated method stub
		ProcurementAdjustment adjustment = dao.findOne(id);
		if (adjustment != null)
			return adjustment;
		else
			return null;
	}

	@Override
	public List<Map<String,Object>> selByCondition(String order_code, String material, String supplier, Integer page,
			Integer pageSize) {
		// TODO Auto-generated method stub
		String sql = "select * from business_purchase_adjustment where 1 = 1";
		if(order_code != null && !order_code.equals("")) {
			sql += " and  order_code like  '%" + order_code + "%'";
		}
		if(material != null && !material.equals("")) {
			sql += " and material like '%" + material + "%'";
		}
		if(supplier != null && !supplier.equals("")) {
			sql += " and supplier like '%" + supplier + "%'";
		}
		  String sqlCount = sql;
		  sql += " limit " + page + "," + pageSize;
		  System.out.println(sql);
		  List<Map<String, Object>> list =   jdbc.queryForList(sql);
		  Map<String,Object> map = new HashMap<String,Object>();
		  map.put("count", jdbc.queryForList(sqlCount).size());
		  list.add(map);
		  return list;
	}

	@Override
	public Boolean save(String order_code, String receiving_unit, String supplier, String settlement_method,
			float number_of_contracts, String create_user_id, String uuid, Integer taskid) {
		// TODO Auto-generated method stub
		ProcurementAdjustment adjustment = new ProcurementAdjustment();
		adjustment.setOrder_code(order_code);
		adjustment.setReceiving_unit(receiving_unit);
		adjustment.setSupplier(supplier);
		adjustment.setCreate_user_id(create_user_id);
		adjustment.setSettlement_method(settlement_method);
		adjustment.setNumber_of_contracts(number_of_contracts);
		adjustment.setUuid(uuid);
		adjustment.setTaskid(taskid);
		adjustment.setAdjustment_amount(0.0f);
		adjustment = dao.save(adjustment);
		if (adjustment != null)
			return true;
		else
			return false;
	}

}
